package com.jpz.controller;


import org.springframework.web.bind.annotation.RequestMapping;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jpz.bean.Depart;
import com.jpz.bean.Emp;
import com.jpz.service.DepartService;
import com.jpz.service.EmpService;
import com.jpz.utils.pageshow;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jpz
 * @since 2018-11-12
 */
@Controller
@RequestMapping("/depart")
public class DepartController {
	@Autowired
	DepartService departservice;
	@Autowired
	EmpService empservice;
	
	@RequestMapping("/show")
	public String showdepart(Integer pageindex,Model model) {
		Page<Depart> page = new Page<>(pageindex, 4);
		Page<Depart> page2 = departservice.selectPage(page, new EntityWrapper<Depart>().eq("del", 0));
		List<Depart> list = page2.getRecords();
		//当前的页面
		int current = page2.getCurrent();
		//共有多少项
		long total = page2.getTotal();
		int m = new Long(total).intValue();
		//获取部门的人数
		for (Depart depart : list) {
			//部门的ID
			int did = depart.getId();
			int count = empservice.selectCount(new EntityWrapper<Emp>().eq("did", did));
			depart.setCount(count);
		}
		//新建一个对象用来回显
		pageshow<Depart> pageshow = new pageshow<>(list,m, null, current);
		//传给页面
		model.addAttribute("pagebean", pageshow);
		return "/departlist.jsp";
		
	}
	@RequestMapping("/update")
	public String update(Integer did,Model model) {
		Depart depart = departservice.selectById(did);
		model.addAttribute("depart", depart);
		return "/departupdate.jsp";
	}
	@RequestMapping("/reallup")
	public String reallup(Depart deprt,Model model,Integer did) {
		boolean byId = departservice.updateById(deprt);
		return "redirect:/depart/show?pageindex=1";
	}
	@RequestMapping("/del")
	public String del(int did,Model model) {
		Depart depart = departservice.selectById(did);
		depart.setDel(1);
		boolean updateById = departservice.updateById(depart);
		return "redirect:/depart/show?pageindex=1";
	}
	@RequestMapping("/add")
	public String add(Depart depart,Model model) {
		departservice.insert(depart);
		return "redirect:/depart/show?pageindex=1";
	}

}

